package com.zhihuichongcheng.application.ui.main.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseFragment;
import com.zhihuichongcheng.application.ui.approval.adapter.ApprovalMainAdapter;
import com.zhihuichongcheng.application.ui.approval.fragment.WaittingApprovalFragment;
import com.zhihuichongcheng.application.ui.mine.UserInfoActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 3water on 2016/11/1.
 */

public class ApprovalFragment extends BaseFragment implements View.OnClickListener{
    private ViewPager mVpApproval;
    private RelativeLayout mRlWaitingApproval,mRlAlreadyApproval,mRlMyApproval,mRlCopytoMe;
    private TextView mTvWaitingApproval,mTvAlreadyApproval,mTvMyApproval,mTvCopytoMe;
    private View mViewWaitingApproval,mViewAlreadyApproval,mViewMyApproval,mViewCopytoMe;
    private List<Fragment> mListData = new ArrayList<>();
    private ApprovalMainAdapter mAdapter;
    private ImageView mImUser;
    @Override
    protected void init() {
        super.init();
        initView();
        initData();
    }

    private void initView()
    {
        mImUser = (ImageView)mView.findViewById(R.id.im_user);
        mVpApproval = (ViewPager)mView.findViewById(R.id.vp_approval);
        mRlWaitingApproval = (RelativeLayout)mView.findViewById(R.id.rl_waiting_approval);
        mTvWaitingApproval = (TextView)mView.findViewById(R.id.tv_waiting_approval);
        mViewWaitingApproval = (View)mView.findViewById(R.id.view_waiting_approval);
        mRlAlreadyApproval = (RelativeLayout)mView.findViewById(R.id.rl_already_approval);
        mTvAlreadyApproval = (TextView)mView.findViewById(R.id.tv_already_approval);
        mViewAlreadyApproval = mView.findViewById(R.id.view_already_approval);
        mRlMyApproval = (RelativeLayout)mView.findViewById(R.id.rl_my_approval);
        mTvMyApproval = (TextView)mView.findViewById(R.id.tv_my_approval);
        mViewMyApproval = mView.findViewById(R.id.view_my_approval);
        mRlCopytoMe = (RelativeLayout)mView.findViewById(R.id.rl_copy_toMe);
        mTvCopytoMe = (TextView)mView.findViewById(R.id.tv_copy_toMe);
        mViewCopytoMe = mView.findViewById(R.id.view_copy_toMe);

        mRlWaitingApproval.setOnClickListener(this);
        mRlAlreadyApproval.setOnClickListener(this);
        mRlMyApproval.setOnClickListener(this);
        mRlCopytoMe.setOnClickListener(this);
        mImUser.setOnClickListener(this);
        mTvWaitingApproval.setTextColor(getResources().getColor(R.color.activity_color));
        mViewWaitingApproval.setVisibility(View.VISIBLE);

    }

    private void initData() {
        for (int i = 0; i < 4; i++)
        {
            WaittingApprovalFragment fragment = new WaittingApprovalFragment();
            mListData.add(fragment);
        }
        mAdapter = new ApprovalMainAdapter(getChildFragmentManager(),mListData);
        mVpApproval.setAdapter(mAdapter);
        mVpApproval.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                    switchIndex(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    public void switchIndex(int index)
    {
        clear();
        switch (index)
        {
            case 0:
                mTvWaitingApproval.setTextColor(getResources().getColor(R.color.activity_color));
                mViewWaitingApproval.setVisibility(View.VISIBLE);
                break;
            case 1:
                mTvAlreadyApproval.setTextColor(getResources().getColor(R.color.activity_color));
                mViewAlreadyApproval.setVisibility(View.VISIBLE);
                break;
            case 2:
                mTvMyApproval.setTextColor(getResources().getColor(R.color.activity_color));
                mViewMyApproval.setVisibility(View.VISIBLE);
                break;
            case 3:
                mTvCopytoMe.setTextColor(getResources().getColor(R.color.activity_color));
                mViewCopytoMe.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }
    }

    private void clear()
    {
        mTvWaitingApproval.setTextColor(getResources().getColor(R.color.color_999999));
        mViewWaitingApproval.setVisibility(View.GONE);
        mTvAlreadyApproval.setTextColor(getResources().getColor(R.color.color_999999));
        mViewAlreadyApproval.setVisibility(View.GONE);
        mTvMyApproval.setTextColor(getResources().getColor(R.color.color_999999));
        mViewMyApproval.setVisibility(View.GONE);
        mTvCopytoMe.setTextColor(getResources().getColor(R.color.color_999999));
        mViewCopytoMe.setVisibility(View.GONE);


    }
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_approval;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.rl_waiting_approval:
                switchIndex(0);
                mVpApproval.setCurrentItem(0,false);
                //待我审批
                break;
            case R.id.rl_already_approval:
                switchIndex(1);
                mVpApproval.setCurrentItem(1,false);
                //我已审批
                break;
            case R.id.rl_my_approval:
                switchIndex(2);
                mVpApproval.setCurrentItem(2,false);
                //我发起的
                break;
            case R.id.rl_copy_toMe:
                switchIndex(3);
                mVpApproval.setCurrentItem(3,false);
                //抄送给我
                break;
            case R.id.im_user:
                startActivity(UserInfoActivity.class,null);
                break;
            default:
                break;
        }
    }
}
